<template>
  <div>
    <!-- 大客户订单审核 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <FilterTables
      ref="table"
      :columns="columns"
      :fetchapi="fetchApi"
      :params="fetchParams"
      :datakey="'records'"
      columnsRef="zjOrderReview"
      :isSelectColumn="true"
      :selectionType="multiple"
      :onRowSelectChange="selectChangeHandle"
      :onColumnsChange="columns => (this.columns = columns)"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 1)" :disabled="isOpen">批量通过</multiuse-button>
        <multiuse-button size="small" type="primary" :click="addHandle.bind(this, 2)" :disabled="isOpen">批量取消</multiuse-button>
      </template>
    </FilterTables>

    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="dealercloseHandler" />
    </BaseDialog>
    <BaseDialog :visible.sync="iscontract" :title="'选择合同'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <contract :rowData="formData" @close="contractClose" />
    </BaseDialog>
    <BaseDialog :visible.sync="isProject" :title="'选择项目'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AccountProject :rowData="formData" @close="ProjectClose" />
    </BaseDialog>

    <Drawer :visible.sync="detailVisible" :title="'大客户订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <detailModel :rowData="formData" @close="deailClose" />
    </Drawer>
    <Drawer :visible.sync="emitVisible" :title="'大客户订单信息'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <emitModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { getList, getNotPage, postorderBatchAudit } from '@/api/omSale/KeyCustomers/OrderReview';
import { dictionary } from '@/utils/dictMixin';
import emitModel from './emitModel';
import detailModel from './detailModel';
import AccountProject from './AccountProject';
import contract from './contract';
import ownerSeacher from './ownerSeacher';
import moment from 'moment';
export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchParams: {
        majorType: 2,
        saveType: 'OEM',
        searchType: 2,
        xhrAbort: true
      },
      fetchApi: getList,
      formData: {},
      detailVisible: false,
      iscontract: false,
      isProject: false,
      isDiscount: false,
      issearch: false,
      emitVisible: false,
      contractId: '',
      ProjectId: '',
      isOpen: true,
      selectList: []
    };
  },
  mixins: [dictionary],
  components: {
    ownerSeacher,
    detailModel,
    AccountProject,
    contract,
    emitModel
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {},
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(3)}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '合同名称',
          fieldName: 'majorContractName',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(1)}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.contractId = '';
            }
          }
        },
        {
          type: 'INPUT',
          label: '大客户项目名称',
          fieldName: 'majorProjectName',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun(2)}></el-button>
              </div>
            );
          },
          change: p => {
            if (!p) {
              this.ProjectId = '';
            }
          }
        },

        {
          type: 'SELECT',
          label: '订单类型',
          fieldName: 'orderType',
          placeholder: '请选择',
          itemList: [
            { text: '直销大客户订单', value: 60061002 },
            { text: '区域大客户订单', value: 60061003 },
            { text: '代交车大客户订单', value: 60061004 }
          ],
          change: p => {
            if (p) {
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
              this.getNotPage(p);
            } else {
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
              this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
            }
          }
        },
        {
          type: 'SELECT',
          label: '订单细分类型',
          fieldName: 'orderDetailType',
          placeholder: '请选择',
          itemList: []
        },
        {
          type: 'INPUT',
          label: '大客户名称',
          fieldName: 'majorCustomerName',
          placeholder: '请输入'
        },
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'orderStatus',
          placeholder: '请选择',
          itemList: [
            { text: '待审核', value: 60431003 },
            { text: '部分审核', value: 60431004 }
          ]
        },
        {
          type: 'INPUT',
          label: '订单编号',
          fieldName: 'orderNo',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '提交日期',
          fieldName: 'reportStartDate|reportEndDate',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 120,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 1)}>
                  审核
                </multiuse-button>
                <el-divider direction="vertical" />
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row, 2)}>
                  详情
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '合同名称',
          dataIndex: 'majorContractName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户项目名称',
          dataIndex: 'majorProjectName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '大客户名称',
          dataIndex: 'majorCustomerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单编号',
          dataIndex: 'orderNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单类型',
          dataIndex: 'orderType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6006'),
          render: p => {
            return <span>{this.createDictText(p.row.orderType, '6006')}</span>;
          }
        },
        {
          title: '订单细分类型',
          dataIndex: 'orderDetailName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单状态',
          dataIndex: 'orderStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: '待审核', value: 60431003 },
            { text: '部分审核', value: 60431004 }
          ],
          render: p => {
            return <span>{this.createDictText(p.row.orderStatus, '6043')}</span>;
          }
        },
        {
          title: '资金类型',
          dataIndex: 'accountTypeName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '运输方式',
          dataIndex: 'transportType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6004'),
          render: p => {
            return <span>{this.createDictText(p.row.transportType, '6004')}</span>;
          }
        },
        {
          title: '订单年月',
          dataIndex: 'orderYearMonth',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        // 新加
        {
          title: '配置名称',
          dataIndex: 'packageName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '外饰颜色',
          dataIndex: 'colorName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '车型名称',
          dataIndex: 'modelName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品代码',
          dataIndex: 'productCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '可用资源',
          dataIndex: 'avilableCount',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{p.row.avilableCount ? p.row.avilableCount : 0}</span>;
          }
        },
        {
          title: '提报数量',
          dataIndex: 'orderTotalNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '已审数量',
          dataIndex: 'checkNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '已取消数量',
          dataIndex: 'cancelNum',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '市场指导价',
          dataIndex: 'beforeSingleAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣后单价',
          dataIndex: 'afterDiscountAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '可用余额',
          dataIndex: 'availableBalance',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{p.row.availableBalance ? p.row.availableBalance : 0}</span>;
          }
        },
        //
        {
          title: '要求交付日期',
          dataIndex: 'requiredDeliveryDate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '订单总金额',
          dataIndex: 'beforeTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '折扣总金额',
          dataIndex: 'discountTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '是否使用返利',
          dataIndex: 'isRebate',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('1004'),
          render: p => {
            return <span>{this.createDictText(p.row.isRebate, '1004')}</span>;
          }
        },
        {
          title: '返利使用总金额',
          dataIndex: 'rebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '单车返利总金额',
          dataIndex: 'singleRebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '建店返利总金额',
          dataIndex: 'buildRebateTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '实付总金额',
          dataIndex: 'realPayTotalAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '审批数量',
          dataIndex: 'checkNums',
          sorter: true,
          filter: true,
          filterType: 'input',
          render: p => {
            return <span>{p.row.checkNum ? p.row.checkNum : 0}</span>;
          }
        },
        {
          title: '提报时间',
          dataIndex: 'reportDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '创建人',
          dataIndex: 'createdName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '创建日期',
          dataIndex: 'createdAt',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    selectChangeHandle(row) {
      if (row.length > 0) {
        this.isOpen = false;
        this.selectList = row || [];
      } else {
        this.isOpen = true;
        this.selectList = [];
      }
    },
    seacherFun(type) {
      if (type == 1) {
        this.iscontract = true;
      } else if (type == 2) {
        this.isProject = true;
      } else {
        this.issearch = true;
      }
    },
    async addHandle(type) {
      if (type == 1) {
        await this.$confirm('是否批量通过?', '提示', {
          confirmButtonText: '通过',
          cancelButtonText: '取消',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectList.map(x => {
              arr.push(x.orderId);
            });
            let json = {
              auditType: 'pass',
              orderIds: arr
            };
            let res = await postorderBatchAudit(json);
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '批量通过成功',
                type: 'success'
              });
              this.isOpen = true;
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      } else if (type == 2) {
        await this.$confirm('是否批量取消?', '提示', {
          confirmButtonText: '是',
          cancelButtonText: '否',
          type: 'warning'
        })
          .then(async () => {
            let arr = [];
            this.selectList.map(x => {
              arr.push(x.orderId);
            });
            let json = {
              auditType: 'cancel',
              orderIds: arr
            };
            let res = await postorderBatchAudit(json);
            if (res.resultCode == 200) {
              this.$notify({
                title: '成功',
                message: '批量取消成功',
                type: 'success'
              });
              this.isOpen = true;
              this.fetchParams = Object.assign({}, this.fetchParams);
            }
          })
          .catch(() => {});
      }
    },
    changeHandle(params) {
      this.fetchParams.majorContractNo = this.contractId;
      this.fetchParams.projectCode = this.ProjectId;
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async editHandle(data, type) {
      if (type == 1) {
        this.formData = data;
        this.emitVisible = true;
      } else if (type == 2) {
        this.formData = data;
        this.detailVisible = true;
      }
    },
    async getNotPage(orderType) {
      let list = await getNotPage({ orderType: orderType });
      this.topFilterList.map(x => {
        if (x.fieldName == 'orderDetailType') {
          x.itemList = list.data.map(x => ({ text: x.subName, value: x.subId }));
        }
      });
    },
    emitcloseHandler(val) {
      if (val) {
        this.fetchParams.majorContractNo = this.contractId;
        this.fetchParams.projectCode = this.ProjectId;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
      this.emitVisible = false;
    },
    deailClose() {
      this.detailVisible = false;
    },
    // 合同返回
    contractClose(val) {
      if (val) {
        this.topFilterList.map(x => {
          if (x.fieldName == 'majorContractName') {
            x.initialValue = val.contractName;
            this.contractId = val.contractNum;
          }
        });
      }
      this.iscontract = false;
    },
    ProjectClose(val) {
      if (val) {
        this.topFilterList.map(x => {
          if (x.fieldName == 'majorProjectName') {
            x.initialValue = val.projectName;
            this.ProjectId = val.projectCode;
          }
        });
      }
      this.isProject = false;
    },
    dealercloseHandler(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },
    resetChange() {
      this.ProjectId = '';
      this.contractId = '';
      this.fetchParams.majorContractNo = '';
      this.fetchParams.projectCode = '';
      this.topFilterList.find(x => x.fieldName == 'orderDetailType').itemList = [];
      this.topFilterList.find(x => x.fieldName == 'orderDetailType').initialValue = '';
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
